<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编码" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单名称" prop="workorderName">
        <el-input
          v-model="queryParams.workorderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="vendorCode">
        <el-input
          v-model="queryParams.vendorCode"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划编码" prop="planCode">
        <el-input
          v-model="queryParams.planCode"
          placeholder="请输入计划编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column label="工单编码" width="150" prop="workorderCode">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)">{{ scope.row.workorderCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="工单名称" width="150" align="center" prop="workorderName" :show-overflow-tooltip="true"/>
      <el-table-column label="计划编号" width="150" align="center" prop="planCode" :show-overflow-tooltip="true"/>
      <el-table-column label="产品编号" width="150" align="center" prop="productCode" :show-overflow-tooltip="true"/>
      <el-table-column label="产品名称" width="150" align="center" prop="productName" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" width="150" align="center" prop="productSpc" :show-overflow-tooltip="true"/>
      <el-table-column label="单位" width="60" align="center" prop="unitOfMeasure"/>
      <el-table-column label="需求日期" align="center" prop="requestDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划开工时间" align="center" width="120px" prop="startDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划完工时间" align="center" width="120px" prop="endDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商编码" align="center" prop="vendorCode" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="供应商" align="center" prop="vendorName" width="150" :show-overflow-tooltip="true"/>
      <el-table-column fixed="right" label="工单数量" width="80" align="center" prop="quantity">
        <template slot-scope="scope">
          <el-tag type="danger">{{ scope.row.quantity }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="状态" align="left" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pro:outsourcing:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单编号">
              <el-input v-model="form.workorderCode" readonly placeholder="请输入工单编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单名称">
              <el-input v-model="form.workorderName" readonly placeholder="请输入工单名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划编号">
              <el-input v-model="form.planCode" readonly placeholder="请输入计划编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划名称">
              <el-input v-model="form.planName" readonly placeholder="请输入计划名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="来源类型" prop="orderSource">
              <el-radio-group v-model="form.orderSource" disabled>
                <el-radio v-for="dict in dict.type.mes_workorder_sourcetype" :key="dict.value" :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.orderSource == 'ORDER'">
            <el-form-item label="订单编号" prop="sourceCode">
              <el-input v-model="form.sourceCode" readonly placeholder="请选择订单"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单据状态" prop="status">
              <el-select v-model="form.status" disabled placeholder="请选择单据状态">
                <el-option
                  v-for="dict in dict.type.mes_order_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编号">
              <el-input v-model="form.productCode" readonly placeholder="请选择产品"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称">
              <el-input v-model="form.productName" placeholder="请选择产品" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号">
              <el-input v-model="form.productSpc" placeholder="请选择产品" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位">
              <el-input v-model="form.unitOfMeasure" placeholder="请选择产品" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="BOM编号">
              <el-input v-model="form.bomCode" placeholder="请选择产品" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="BOM版本">
              <el-input v-model="form.bomName" placeholder="请选择产品" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划开工日期">
              <el-date-picker
                clearable
                readonly
                v-model="form.startDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择计划开工日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划完工日期">
              <el-date-picker
                clearable
                readonly
                v-model="form.endDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择计划完工日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单数量">
              <el-input-number :min="1" v-model="form.quantity" disabled placeholder="请输入生产数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="需求日期" prop="requestDate">
              <el-date-picker
                clearable
                readonly
                v-model="form.requestDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择需求日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col></el-col>
          <el-col></el-col>
          <el-col></el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" readonly placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="(optType == 'view' || form.status != 'PREPARE') && optType != 'finish'">返 回</el-button>
        <el-button type="primary" @click="submitForm" v-if="form.status == 'PREPARE' && optType != 'view'">保 存</el-button>
        <el-button type="primary" @click="handDofinish" v-if="optType == 'finish'">完 工</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addPurchase, delPurchase, getPurchase, listPurchase, updatePurchase } from "@/api/mes/pro/purchase";

export default {
  name: "Purchase",
  dicts: ['mes_order_status', 'mes_workorder_sourcetype', 'mes_workorder_type'],
  data() {
    return {
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产工单--外购表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: null,
        workorderName: null,
        workorderType: null,
        orderSource: null,
        sourceCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpc: null,
        unitOfMeasure: null,
        quantity: null,
        quantityProduced: null,
        quantityChanged: null,
        quantityScheduled: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        beginDate: null,
        finishDate: null,
        parentId: null,
        ancestors: null,
        startDate: null,
        endDate: null,
        requestDate: null,
        status: null,
        batchCode: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        planCode: null,
        planName: null,
        bomId: null,
        bomCode: null,
        bomName: null,
        tenantId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workorderCode: [
          {required: true, message: "工单编码不能为空", trigger: "blur"}
        ], workorderName: [
          {required: true, message: "工单名称不能为空", trigger: "blur"}
        ], orderSource: [
          {required: true, message: "来源类型不能为空", trigger: "blur"}
        ], productId: [
          {required: true, message: "产品ID不能为空", trigger: "blur"}
        ], productCode: [
          {required: true, message: "产品编号不能为空", trigger: "blur"}
        ], productName: [
          {required: true, message: "产品名称不能为空", trigger: "blur"}
        ], unitOfMeasure: [
          {required: true, message: "单位不能为空", trigger: "blur"}
        ], quantity: [
          {required: true, message: "生产数量不能为空", trigger: "blur"}
        ], parentId: [
          {required: true, message: "父工单不能为空", trigger: "blur"}
        ], ancestors: [
          {required: true, message: "所有父节点ID不能为空", trigger: "blur"}
        ], requestDate: [
          {required: true, message: "需求日期不能为空", trigger: "blur"}
        ], planCode: [
          {required: true, message: "计划编码不能为空", trigger: "blur"}
        ], planName: [
          {required: true, message: "计划名称不能为空", trigger: "blur"}
        ], bomId: [
          {required: true, message: "BOM ID不能为空", trigger: "blur"}
        ], bomCode: [
          {required: true, message: "BOM编码不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产工单--外购列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workorderId: null,
        workorderCode: null,
        workorderName: null,
        workorderType: null,
        orderSource: null,
        sourceCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpc: null,
        unitOfMeasure: null,
        quantity: null,
        quantityProduced: null,
        quantityChanged: null,
        quantityScheduled: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        beginDate: null,
        finishDate: null,
        parentId: null,
        ancestors: null,
        startDate: null,
        endDate: null,
        requestDate: null,
        status: "0",
        batchCode: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        planCode: null,
        planName: null,
        bomId: null,
        bomCode: null,
        bomName: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const workorderId = row.workorderId || this.ids;
      getPurchase(workorderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '查看工单信息';
        this.optType = 'view';
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workorderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产工单--外购";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workorderId = row.workorderId || this.ids
      getPurchase(workorderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改外购工单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs[ "form" ].validate(valid => {
        if (valid) {
          if (this.form.workorderId != null) {
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workorderIds = row.workorderId || this.ids;
      this.$modal.confirm('是否确认删除外购工单编号为"' + workorderIds + '"的数据项？').then(function () {
        return delPurchase(workorderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/purchase/export', {
        ...this.queryParams
      }, `purchase_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
